package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.StudentAtSchool;
import com.xmy.cultivate.entity.StudentReadSchool;
import com.xmy.cultivate.service.IQuarterService;
import com.xmy.cultivate.service.IStudentAtSchoolService;
import com.xmy.cultivate.service.IStudentReadSchoolService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.CommonUtil;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 学员在读公立学校 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2024-10-21
 */
@RestController
@RequestMapping("/api/studentReadSchool")
public class StudentReadSchoolController {
    @Autowired
    IStudentReadSchoolService iStudentReadSchoolService;

    @Autowired
    IQuarterService iQuarterService;

    @Autowired
    IStudentAtSchoolService iStudentAtSchoolService;

    @GetMapping("")
    public Result findById(@RequestParam(value = "yearClassId", required = false) String yearClassId,
                           @RequestParam(value = "studentId", required = false) String studentId) {
        Integer gradeSectionId = CommonUtil.getGradeSectionId(Integer.valueOf(yearClassId));
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("grade_section", gradeSectionId);
        wrapper.eq("student_id", studentId);
        wrapper.last("LIMIT 1");
        StudentReadSchool studentReadSchool = iStudentReadSchoolService.getOne(wrapper);

        if (studentReadSchool != null) {
            StudentAtSchool studentAtSchool = iStudentAtSchoolService.getById(studentReadSchool.getReadingSchoolId());
            if (studentAtSchool != null) {
                studentReadSchool.setReadingSchoolName(studentAtSchool.getName());
            }
        }

        return new Result(Code.OK, studentReadSchool, Code.GET_OK_MSG);
    }

}
